package com.hmitkov.android.tabme;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {
	private static final int TIME = 5;
	ImageView btnTab;
	TextView tvTime, tvScore;
	int numClicks = 0;
	int seconds = TIME;
	MediaPlayer mpTab;
	MediaPlayer mpSong;
	MediaPlayer mpTime;
	final Handler handler = new Handler();
	final Timer timer = new Timer();
	boolean running = false;
	final Runnable changeTime = new Runnable() {
		@Override
		public void run() {
			if (mpTime != null && mpTime.isPlaying()) {
				mpTime.stop();
				mpTime.release();
			}
			if (!running) {
				return;
			}
			mpTime = MediaPlayer.create(MainActivity.this, R.raw.second_beep);
			mpTime.start();
			tvTime.setText("Time: " + seconds);
			seconds--;
			if (seconds < 0) {
				gameEnded();
			}
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		tvTime = (TextView) findViewById(R.id.etTime);
		tvScore = (TextView) findViewById(R.id.etScore);
		btnTab = (ImageView) findViewById(R.id.btnTab);

		seconds = TIME;
		tvScore.setText("Score\n" + numClicks);

		btnTab.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (mpTab != null) {
					mpTab.stop();
					mpTab.release();
				}
				mpTab = MediaPlayer.create(MainActivity.this, R.raw.button_tap);
				mpTab.start();
				numClicks++;
				tvScore.setText("Score\n" + numClicks);
			}
		});

	}

	@Override
	public void onResume() {
		super.onResume();
		startGame();
	}

	@Override
	public void onStop() {
		super.onStop();
		stopMusic();
	}

	TimerTask task = null;

	private void startMusic() {
		mpSong = MediaPlayer.create(MainActivity.this, R.raw.hall_of_the_mountain_king);
		mpSong.start();
	}

	private void stopMusic() {
		if (mpSong != null) {
			mpSong.release();
			mpSong = null;
		}
	}
private void startGame(){
	seconds = TIME;
	numClicks = 0;
	startTimer();
	startMusic();
	btnTab.setEnabled(true);
}
	private void gameEnded() {
		running = false;
		stopMusic();
		btnTab.setEnabled(false);
		showPlayAgainDialog();
	}

	private void startTimer() {
		running = true;
		for (int i = 0; i <= seconds; i++) {
			task = new TimerTask() {
				@Override
				public void run() {
					handler.post(changeTime);
				}
			};
			timer.schedule(task, i * 1000);
		}
	}

	private void showPlayAgainDialog() {
		AlertDialog alertDialog = new AlertDialog.Builder(this).create();
		alertDialog.setTitle("Time Is Up");
		alertDialog.setMessage(Html.fromHtml("<center><b>Your score is:</b> " + numClicks + " points<br /><br />Play again?<center>"));
		// Setting OK Button
		alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "YES", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				startGame();
			}
		});
		alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "NO", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
			}
		});

		// Showing Alert Message
		alertDialog.show();
	}
}